﻿using System;
using System.Drawing;
using RibbonLib.Interop;
using pub.NET20Fix;

namespace todo.Ribbon
{
    public class RibbonColorPicker : RibbonControl
    {
        public event Action OnSelect;

        public void FireOnSelect()
        {
            var handler = OnSelect;
            if (handler != null) handler();
        }

        private SwatchColorType? _colorType;
        private Color? _color;
        private Color[] _standardColors;
        private Color[] _themeColors;

        public RibbonColorPicker(RibbonUI ribbonUI, uint commandID) : base(ribbonUI, commandID)
        {
        }

        public Color Color
        {
            get
            {
                PropVariant color;
                var hr = _ribbonUI.Framework.GetUICommandProperty(_commandID,
                        ref RibbonProperties.Color, out color);
                if (NativeMethods.Succeeded(hr))
                {
                    return ColorTranslator.FromWin32((int) (uint) color.Value);
                }

                return _color.GetValueOrDefault();
            }
            set
            {
                _color = value;
                var color = PropVariant.FromObject((uint) ColorTranslator.ToWin32(value));
                var hr = _ribbonUI.Framework.SetUICommandProperty(_commandID,
                        ref RibbonProperties.Color, ref color);
            }
        }

        public SwatchColorType ColorType
        {
            get
            {
                PropVariant colorType;
                var hr = _ribbonUI.Framework.GetUICommandProperty(_commandID,
                        ref RibbonProperties.ColorType, out colorType);
                if (NativeMethods.Succeeded(hr))
                {
                    return (SwatchColorType) (uint) colorType.Value;
                }


                return _colorType.GetValueOrDefault();
            }
            set
            {
                _colorType = value;

                var colorType = PropVariant.FromObject((uint) value);
                var hr = _ribbonUI.Framework.SetUICommandProperty(_commandID,
                        ref RibbonProperties.ColorType, ref colorType);
            }
        }

        public Color[] StandardColors
        {
            get { return _standardColors; }
            set
            {
                _standardColors = value;
                var hr = _ribbonUI.Framework.InvalidateUICommand(_commandID, Invalidations.Property,
                        PropertyKeyRef.From(RibbonProperties.StandardColors));
            }
        }

        public Color[] ThemeColors
        {
            get { return _themeColors; }
            set
            {
                _themeColors = value;
                var hr = _ribbonUI.Framework.InvalidateUICommand(_commandID, Invalidations.Property,
                        PropertyKeyRef.From(RibbonProperties.ThemeColors));
            }
        }

        public override HRESULT UpdateProperty(ref PropertyKey key, PropVariantRef currentValue,
                                               ref PropVariant newValue)
        {
            if (key == RibbonProperties.Color)
            {
                if (_color.HasValue)
                {
                    newValue.SetUInt((uint) ColorTranslator.ToWin32(_color.Value));
                }
            }
            else if (key == RibbonProperties.ColorType)
            {
                if (_colorType.HasValue)
                {
                    newValue.SetUInt((uint) _colorType.Value);
                }
            }
            else if (key == RibbonProperties.StandardColors)
            {
                if (_standardColors != null)
                {
                    var intColors = Array.ConvertAll(_standardColors, ColorTranslator.ToWin32);
                    var uintColors = Array.ConvertAll(intColors, Convert.ToUInt32);
                    newValue.SetUIntVector(uintColors);
                }
            }
            else if (key == RibbonProperties.ThemeColors)
            {
                if (_themeColors != null)
                {
                    var intColors = Array.ConvertAll(_themeColors, ColorTranslator.ToWin32);
                    var uintColors = Array.ConvertAll(intColors, Convert.ToUInt32);
                    newValue.SetUIntVector(uintColors);
                }
            }
            return base.UpdateProperty(ref key, currentValue, ref newValue);
        }

        public override HRESULT Execute(ExecutionVerb verb, PropertyKeyRef key,
                                        PropVariantRef currentValue,
                                        IUISimplePropertySet commandExecutionProperties)
        {
            if (verb == ExecutionVerb.Execute)
            {
                FireOnSelect();
            }
            return base.Execute(verb, key, currentValue, commandExecutionProperties);
        }
    }
}